package com.blue.eagle.dao;

import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.blue.eagle.domain.Customer;

@Repository
@Transactional(readOnly = true)
public class CustomerDaoImpl implements CustomerDao {

	/**
	 * @uml.property  name="entityManager"
	 * @uml.associationEnd  readOnly="true"
	 */
	@PersistenceContext
	private EntityManager entityManager ;
	
	
	@Transactional (readOnly = false,propagation = Propagation.REQUIRES_NEW)
	public void save(Customer customer) {
		System.out.println("Object Saved Perfectly !");
		entityManager.persist(customer);
	}

	@Transactional (readOnly = false,propagation = Propagation.REQUIRES_NEW)
	public Customer update(Customer customer) {
		return entityManager.merge(customer);
	}

	@SuppressWarnings("unchecked")
	@Transactional (readOnly = true)
	public Collection<Customer> getCustomerList() {
		System.out.println("Finding all Customers ");
		List<Customer> ls = entityManager.createQuery("select c from Customer as c").getResultList();
		System.out.println("Size List : "+ls.size());
		return ls;
	}

	
	
}
